﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;

namespace WatiN.Sharepoint.WebControls
{
    public class HeaderColumn : Control<TableHeaderCell>
    {
        private TableHeaderCell _tableCell;
        private int _index;

        public HeaderColumn(TableHeaderCell tableCell, int index)
        {
            this._tableCell = tableCell;
            this._index = index;
        }

        public string Title
        {
            get 
            {
                if (_tableCell.ClassName.Equals("ms-vh-icon", StringComparison.InvariantCultureIgnoreCase))
                {
                    return "";
                }
                else
                {
                    Div innerDiv = _tableCell.Div(Find.ByClass("ms-vh-div"));
                    Link columnLink = null;

                    if (innerDiv.Elements[0] is Table) 
                    {
                        var headertextCell = innerDiv.Tables[0].TableCell(Find.ByClass("ms-vh ms-imnTxtTD"));

                        columnLink = headertextCell.Links[0];
                    }
                    else if (innerDiv.Elements[0] is Link)
                    {
                        columnLink = innerDiv.Links[0];
                    }

                    return columnLink.Text;
                }
            }
        }

    }
}
